package com.scsj.supermarket.view.activity.shopmodel;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.GoodsSortAdapter;
import com.scsj.supermarket.bean.GoodsSortBean;
import com.scsj.supermarket.customview.GridSpacingItemDecoration;
import com.scsj.supermarket.interfaces.GoodsType;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.scsj.supermarket.view.fragment.goodssortmodel.FilterFragment;

import java.util.ArrayList;
import java.util.List;

import static com.scsj.supermarket.adapter.GoodsSortAdapter.GRID;
import static com.scsj.supermarket.adapter.GoodsSortAdapter.LINEAR;


/**
 *
 */
public class GoodsSortActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout back_ll, search_ll, array_type_ll;
    private ImageView array_type_iv;
    private int arrayType = GRID;//排列方式 0 网格 1 列表
    //筛选商品
    private DrawerLayout drawer_layout;
    private FrameLayout drawer_content;
    //排序 筛选
    private LinearLayout recommend_ll, sales_ll, price_ll, filtrate_ll;
    private TextView recommend_tv, sales_tv, price_tv, filtrate_tv;
    private ImageView recommend_iv, price_iv, filtrate_iv;
    private RecyclerView recycler_view;
    private GridSpacingItemDecoration decor;
    private GoodsSortAdapter goodsSortAdapter;
    private List<GoodsSortBean> mDatas;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_goods_sort);
    }

    @Override
    protected void initView() {
        back_ll = findViewById(R.id.back_ll);
        search_ll = findViewById(R.id.search_ll);
        array_type_ll = findViewById(R.id.array_type_ll);
        array_type_iv = findViewById(R.id.array_type_iv);
        //抽屉
        drawer_layout = findViewById(R.id.drawer_layout);
        drawer_content = findViewById(R.id.drawer_content);

        Fragment fragment = new FilterFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        Bundle bundle = new Bundle();
        bundle.putString("departmentName", "");
        fragment.setArguments(bundle);
        fragmentManager.beginTransaction().replace(R.id.drawer_content, fragment).commit();
        //推荐
        recommend_ll = findViewById(R.id.recommend_ll);
        recommend_tv = findViewById(R.id.recommend_tv);
        recommend_iv = findViewById(R.id.recommend_iv);
        //销量
        sales_ll = findViewById(R.id.sales_ll);
        sales_tv = findViewById(R.id.sales_tv);
        //价格
        price_ll = findViewById(R.id.price_ll);
        price_tv = findViewById(R.id.price_tv);
        price_iv = findViewById(R.id.price_iv);
        //筛选
        filtrate_ll = findViewById(R.id.filtrate_ll);
        filtrate_tv = findViewById(R.id.filtrate_tv);
        filtrate_iv = findViewById(R.id.filtrate_iv);
        recycler_view = findViewById(R.id.recycler_view);
        decor = new GridSpacingItemDecoration(2, 6, false);
        goodsSortAdapter = new GoodsSortAdapter(this);
        goodsSortAdapter.setArrayType(arrayType);
        recycler_view.setAdapter(goodsSortAdapter);
        if (arrayType == GRID) {
            recycler_view.setLayoutManager(new GridLayoutManager(this, 2));
            recycler_view.addItemDecoration(decor);
        } else {
            recycler_view.setLayoutManager(new LinearLayoutManager(this));
            recycler_view.removeItemDecoration(decor);
        }
    }
    @Override
    protected void initListener() {
        back_ll.setOnClickListener(this);
        search_ll.setOnClickListener(this);
        array_type_ll.setOnClickListener(this);
        filtrate_ll.setOnClickListener(this);

        goodsSortAdapter.setOnItemClickListener(new GoodsSortAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, GoodsSortBean data, int postion) {
                SkipUtils.toGoodsDetails(mContext, GoodsType.common);
            }
        });
    }

    @Override
    protected void initData() {
        mDatas = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mDatas.add(new GoodsSortBean("红酒" + i));
        }
        goodsSortAdapter.addAllData(mDatas);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.back_ll:
                finish();
                break;
            case R.id.search_ll:

                break;
            case R.id.array_type_ll:
                if (arrayType == GRID) {
                    arrayType = LINEAR;
                    array_type_iv.setImageResource(R.mipmap.ic_array_grid);
                    goodsSortAdapter.setArrayType(LINEAR);
                    recycler_view.setLayoutManager(new LinearLayoutManager(this));
                    recycler_view.removeItemDecoration(decor);
                    goodsSortAdapter.notifyDataSetChanged();
                } else {
                    arrayType = GRID;
                    array_type_iv.setImageResource(R.mipmap.ic_array_vertical);
                    goodsSortAdapter.setArrayType(GRID);
                    recycler_view.setLayoutManager(new GridLayoutManager(this, 2));
                    recycler_view.addItemDecoration(decor);
                    goodsSortAdapter.notifyDataSetChanged();
                }
                break;
            case R.id.filtrate_ll:
                drawer_layout.openDrawer(drawer_content);
                break;
            default:
                break;
        }
    }
}
